angular.module('furion.controllers.session', [])

.controller 'SigninCtrl', ($scope, $rootScope, $cordovaDevice, $state, RemoteSrv, Toast, GestureService, TouchAndGestureService, AuthNotify)->
  $scope.account = {} # load from localstorage
  $scope.canSubmit = false

  $scope.open_modal = (name)->
    $scope.$emit("modals:open", {modal_name: name, params: $scope.modal_params})

  $scope.changeInput = (type)->
    if $scope.account.phone && $scope.account.password
      $scope.canSubmit = true
    else
      $scope.canSubmit = false

  $scope.doSignin = ()->
    res = furion.utils.verifyPhone($scope.account.phone)
    if !res.result
      Toast.show res.data
      return
    res = furion.utils.verifyPassword($scope.account.password)
    if !res.result
      Toast.show res.data
      return

    if furion.utils.isDevice()
      device = $cordovaDevice.getDevice()
      dmodel = $cordovaDevice.getModel()
      dplatform = $cordovaDevice.getPlatform()
      dversion = $cordovaDevice.getVersion()
      duuid = $cordovaDevice.getUUID()
    else
      device = "browser"
      dmodel = "browser"
      dplatform = "browser"
      dversion = "browser"
      duuid = ""

    params =
      account:
        phone: $scope.account.phone
        password: $scope.account.password
      ios_token: furion.storage.deviceInfo().ios_token
      uuid: if device then device.uuid else undefined
      jpush_token: furion.storage.deviceInfo().jpush_token
      stat: {model: dmodel, platform: dplatform, version: dversion, uuid: duuid}

    $scope.$emit "loading:show"
    RemoteSrv.signin(params).always( ()->
      $scope.$emit "loading:hide"
    ).done( (resp) ->
      console.debug 'signin'
      if resp.status.success
        params =
          account: resp.data
          scope: $scope
          state: $state
          params: $scope.modal_params
          gs: GestureService
          toast: Toast
        furion.storage.accountLogin params
        AuthNotify.resetNotifiedAuth()
        TouchAndGestureService.suggestUserSetting()
    )
  return
# end of SigninCtrl

.controller 'SignupCtrl', ($scope, $rootScope, $http, RemoteSrv, Toast, WizardHandler, $state, $sce, GestureService, $ionicModal, VcodeService, $cordovaDevice, Config, TouchAndGestureService, Geolocation) ->
  $scope.getPosition = Geolocation.getPosition()
  $scope.getLocation = $scope.getPosition.get
  $scope.cityName = window.localStorage.getItem('geolocation-cityname')

  $scope.account =
    phone: ''
    password: ''
    promotion_code: ''
  $scope.canSend = false
  $scope.canSubmit = false
  # $scope.timeClick = false
  $scope.btnCodeHint = '发送验证码'
  $scope.interval = 0
  timer = null

  $scope.showAgreement = ()->
    $scope.$emit("modals:open", {modal_name: 'signup_agreement'})

  $scope.$on "$destroy", (event)->
    clearInterval(timer) if timer

  $scope.changeFirstInput = ()->
    if $scope.account.phone && $scope.account.password
      $scope.canSend = true
    else
      $scope.canSend = false

  checkFirstInput = ()->
    result = furion.utils.verifyPhone($scope.account.phone)
    if !result.result
      Toast.show result.data
      return false

    result = furion.utils.verifyPassword($scope.account.password)
    if !result.result
      Toast.show result.data
      return false
    return true

  $scope.sendCode = ()->
    return if false == checkFirstInput()
    RemoteSrv.getValidationCode($scope.account.phone, 'signup').done (resp) ->
      if resp.status.success
        VcodeService.setRejectWaiting('signup', resp.data.interval)
        $scope.vcode_reject_waiting = $rootScope.vcode_reject_waiting
        Toast.show resp.data.vcode, 'long' if resp.data.vcode # debug 模式下可以看到验证码
        WizardHandler.wizard().goTo(1)
      else
        Toast.show resp.status.msg

  $scope.previewStep = ()->
    WizardHandler.wizard().previous()

  $scope.changeCodeInput = ()->
    if $scope.account.vcode
      $scope.canSubmit = true
    else
      $scope.canSubmit = false

  $scope.doSignup = ()->
    result = furion.utils.verifySmsCode($scope.account.vcode)
    if !result.result
      Toast.show result.data
      return

    if furion.utils.isDevice()
      device = $cordovaDevice.getDevice()
      dmodel = $cordovaDevice.getModel()
      dplatform = $cordovaDevice.getPlatform()
      dversion = $cordovaDevice.getVersion()
      duuid = $cordovaDevice.getUUID()
    else
      device = "browser"
      dmodel = "browser"
      dplatform = "browser"
      dversion = "browser"
      duuid = ''

    params =
      account:
        phone: $scope.account.phone
        password: $scope.account.password
        promotion_code: $scope.account.promotion_code
      vcode: $scope.account.vcode
      ios_token: furion.storage.deviceInfo().ios_token
      jpush_token: furion.storage.deviceInfo().jpush_token
      stat: {model: dmodel, platform: dplatform, version: dversion, uuid: duuid}


    $scope.$emit 'loading:show'
    RemoteSrv.signup(params).always( ()->
      $scope.$emit 'loading:hide'
    ).done( (resp) ->
      params =
        account: resp.data
        scope: $scope
        state: $state
        params: $scope.modal_params
        gs: GestureService
        toast: Toast
      furion.storage.accountLogin params
      AuthNotify.resetNotifiedAuth()
      TouchAndGestureService.suggestUserSetting()
    )
  return
# end of SignupCtrl






